package com.joney.backstage.controller;

import com.joney.backstage.constant.Constants;
import com.joney.backstage.pojo.MenuTree;
import com.joney.backstage.service.ResourceService;
import com.joney.common.pojo.UserSession;
import com.joney.common.utils.EncryptUtil;
import com.joney.common.utils.JwtToken;
import com.joney.common.utils.SecurityCodeUtil;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author wangxianchen
* @create 2018-01-15
* @desc 首页控制器
*/
@Controller
public class IndexController {


    @Autowired
    private ResourceService resourceService;

    @Autowired
/*    @Qualifier("wangxianchen")*/
    StringEncryptor stringEncryptor;


    /**
     * @desc 加密
     * @author wangxianchen
     * @create 2018-01-16
     * @param pwd
     */
    //@GetMapping(value = "/enc")
    //public void encryptPwd(String pwd) {
    //    String result = stringEncryptor.encrypt(pwd);
    //    System.out.println("--------------------->"+result+"<---------------------");
    //}

    /**
     * @desc 解密
     * @author wangxianchen
     * @create 2018-01-16
     * @param
     */
    @GetMapping(value = "/dec")
    @ResponseBody
    public void decryptPwd(String pwd) {
        String result = stringEncryptor.decrypt(pwd);
        System.out.println("--------------------->"+result+"<---------------------");
    }

    @GetMapping("/main")
    public String index(ModelMap model, HttpServletRequest request) {
        UserSession userSession = JwtToken.getLoginUser(request);
        List<MenuTree> treeList = resourceService.getNavigationTree(userSession.getUid().intValue());
        model.put("treeList",treeList);
        model.put("loginUser", userSession);
        model.put("cryptoKey", EncryptUtil.AES_ENCRYPT_KEY);
        return "sys_base/main";
    }


    @RequestMapping("/home")
    public String home(ModelMap model) {
        return "sys_base/home";
    }

    @RequestMapping("/login")
    public String login(ModelMap model) {
        model.put("cryptoKey", EncryptUtil.AES_ENCRYPT_KEY);
        return "sys_base/login";
    }

    @RequestMapping("/check_code")
    public void checkcode(@RequestParam long checkId,HttpServletResponse response) {
        try {
            SecurityCodeUtil.Code code = SecurityCodeUtil.generateCode("6","num","130","40");
            CacheManager manager = CacheManager.getInstance();
            Cache cache = manager.getCache(Constants.CACHE_KEY_OF_CHECK_CODE);
            cache.put(new Element(checkId, code.getContent()));
            ImageIO.write(code.getImage(),"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/403")
    public String forbidden(ModelMap model) {
        return "sys_base/403";
    }

    @RequestMapping("/404")
    public String pageNotFound(ModelMap model) {
        return "sys_base/404";
    }
    @RequestMapping("/blank")
    public String blank(ModelMap model) {
        return "sys_base/blank";
    }
    @RequestMapping("/token_invalid")
    public String invalid(ModelMap model) {
        return "sys_base/invalid";
    }


}
